package com.ch.light.web.service.impl;

import com.ch.light.core.IdGenerator;
import com.ch.light.mybatis.ParamFilter;
import com.ch.light.web.dao.RoleMenuDao;
import com.ch.light.web.mapper.RoleMapper;
import com.ch.light.web.model.Role;
import com.ch.light.web.service.RoleService;
import com.ch.light.web.util.UserContextUtil;
import com.ch.utils.CommonUtils;
import com.github.pagehelper.PageHelper;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Resource
    private RoleMenuDao roleMenuDao;

    @Override
    public void add(Role role) {
        checkNotNull(role, "角色信息不能为空");
        checkArgument(!Strings.isNullOrEmpty(role.getName()), "角色名称不能为空");
        checkArgument(!Strings.isNullOrEmpty(role.getCode()), "角色标识不能为空");
        String code = role.getCode();
        Role record = this.findByCode(code);
        checkArgument(record == null, "角色编号已经存在");
        String roleId = IdGenerator.getInstance().nextId();
        role.setNo(roleId);
        role.setCreateAt(new Date());
        role.setCreateBy(UserContextUtil.getUserId());
        roleMapper.insertSelective(role);
    }

    @Override
    public void delete(List<String> roleIds) {
        List<Role> records = Lists.newArrayList();
        for (String roleId : roleIds) {
            Role model = this.getByRoleId(roleId);
            checkNotNull(model, "角色对象不存在");
            records.add(model);
        }
        records.forEach(r -> {
            roleMapper.delete(r);
            roleMenuDao.update("deleteByRoleId", r.getNo());
        });
    }

    @Override
    public void update(Role record) {
        checkNotNull(record, "角色信息不能为空");
        checkArgument(!Strings.isNullOrEmpty(record.getName()), "角色名称不能为空");
        checkArgument(!Strings.isNullOrEmpty(record.getCode()), "角色标识不能为空");

        Role model = this.getByRoleId(record.getNo());
        checkNotNull(model, "角色对象不存在");

        Role r = findByCode(record.getCode());
        checkArgument(Objects.equals(model.getId(), r.getId()), "角色标识已存在");

        record.setUpdateAt(new Date());
        record.setUpdateBy(UserContextUtil.getUserId());
        BeanUtils.copyProperties(record, model);
        roleMapper.updateByPrimaryKeySelective(model);
    }

    @Override
    public List<Role> getList(ParamFilter paramFilter) {
        int pageNum = paramFilter.getPage().getPageNo();
        int pageSize = paramFilter.getPage().getPageSize();
        PageHelper.startPage(pageNum, pageSize, true, false, false);
        Example example = new Example(Role.class);
        Map<String, Object> param = paramFilter.getParam();
        return roleMapper.selectByExample(example);
    }

    @Override
    public Role getByRoleId(String roleId) {
        checkArgument(!Strings.isNullOrEmpty(roleId), "角色编号不能为空");
        Role param = new Role();
        param.setNo(roleId);
        // checkNotNull(role, "角色对象不存在");
        return roleMapper.selectOne(param);
    }

    @Override
    public Role findByCode(String code) {
        checkArgument(!Strings.isNullOrEmpty(code), "角色标识不能为空");
        Role param = new Role();
        param.setCode(code);
        return roleMapper.selectOne(param);
    }

    @Override
    public List<Role> findByStatus(String status) {
        Role param = new Role();
        param.setStatus(status);
        return roleMapper.select(param);
    }

    @Override
    public List<Role> findByUserId(String userId) {
        checkArgument(!Strings.isNullOrEmpty(userId), "用户编号不能为空");
        return roleMapper.findByUserId(userId);
    }

    @Override
    public int removeAssignMenuByRoleId(String roleId) {
        return roleMapper.removeAssignMenuByRoleId(roleId);
    }

    @Override
    public void assignMenus(String roleId, List<String> menuIdList) {
        checkArgument(!Strings.isNullOrEmpty(roleId), "角色编号不能为空");
        checkArgument(!CommonUtils.isEmpty(menuIdList), "菜单编号不能为空");
        menuIdList.forEach(r -> roleMapper.assignMenu(roleId, r));

    }

    @Override
    public void reassignMenus(String roleId, List<String> menuIdList) {
        checkArgument(!CommonUtils.isEmpty(roleId), "角色编号不能为空");
        checkArgument(!CommonUtils.isEmpty(menuIdList), "菜单编号不能为空");
        menuIdList.forEach(r -> roleMapper.reassignMenu(roleId, r));
    }

}
